package Test;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

public class Calculator extends JFrame implements ActionListener
{
    //create objects
    JLabel product;
    JLabel department;
    JLabel originalPrice;
    JLabel discount;
    JLabel finalPrice;
    JTextField productName;
    JComboBox<String> departments;
    JTextField oPrice;
    JTextField discPercent;
    JTextField salePrice;
    JButton calc;
    JButton clear;
    JButton exit;

    private String[] _titles = new String[] {"Item Name", "Department","Original Price","Sale Price"};
    private String[][] _data = new String[][] {{"", "","",""},{"","","",""}};


    public Calculator()
    {
        //Frame
        super("Retail Calculator");
        setSize(800, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout(FlowLayout.CENTER));


        //Labels and Fields
        product = new JLabel("Product Name");
        productName = new JTextField(10);//allows user to enter item name 
        department = new JLabel("Department");
        departments = new JComboBox<String>();
           departments.addItem("-Select One-");//allows user to choose items department
           departments.addItem("Mens");
           departments.addItem("Womens");
           departments.addItem("Electronics");
           departments.addItem("Hardware");
           departments.addItem("Shoes");

        originalPrice = new JLabel("Original Price");
        oPrice = new JTextField(10);//allows user to enter original price 
        discount = new JLabel("Discount %");
        discPercent = new JTextField(10);//allows user to enter percent of discount 
        finalPrice = new JLabel("Sale Price");
        salePrice = new JTextField(10);//shows user the calculated sale price 
        salePrice.setEditable(false);

             //Buttons
            calc = new JButton("Calculate");//calculates user input 
            clear = new JButton("Clear");//clears user input
            exit = new JButton("Exit");//exits application




    //add items to Frame
        add(product);
        add(productName);
        add(department);
        add(departments);
        add(originalPrice);
        add(oPrice);
        add(discount);
        add(discPercent);
        add(finalPrice);
        add(salePrice);
        add(calc);
        add(clear);
        add(exit);

        //Table for results(found example that helped at- http://www.gammelsaeter.com/programming/simple-jframe-and-jtable-example-in-java/)
        JTable results = new JTable(_data, _titles);
        JScrollPane scrollPane = new JScrollPane(results);
        getContentPane().add(scrollPane, BorderLayout.CENTER);
        setVisible(true);


        calc.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent retail) {
                String input1;
                String input2;
                double origPrice;
                double percOff;
                double clearance;

                input1 = oPrice.getText();
                input2 = discPercent.getText();
                origPrice = Double.parseDouble(input1);
                percOff = Double.parseDouble(input2)/100;
                clearance = origPrice - (origPrice * percOff);
                DecimalFormat df = new DecimalFormat("$#,###.##");                     
                df.format(clearance);                       
                salePrice.setText(df.format(clearance));
            }
        });

        clear.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event){
                productName.setText(null);
                departments.setSelectedIndex(0);
                oPrice.setText(null);
                discPercent.setText(null);
                salePrice.setText(null);
            }
        });

        exit.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent event){
                System.exit(0);
            }
        });





        setVisible(true);
    }

    public static void main(String[] args) 
       {
            new Calculator();
        }


        public void actionPerformed(ActionEvent arg0) {
            System.exit(0);
    }

}